from django.shortcuts import render
from django.views.generic import ListView
from .form import PostForm
from myapp.models import Post
from django.views.generic import DetailView
from django.views.generic.edit import FormView
from django.views.generic.edit import CreateView
from django.views.generic.edit import UpdateView
from django.views.generic.edit import DeleteView
from django.http import HttpResponse


def myapp(request):
    return render(request, 'myapp.html')

class LListVV(ListView):
    #设置模版文件
    template_name = 'LListV.html'
    #设置模型外的数据
    extra_context = {'title':'boke'}
    #查询模型外的数据
    queryset = Post.objects.all()
    #每页展示一条数据
    #paginate_by =1
    #若不设置，则模版上下文默认为persioninfo_list
    #context_object_name='personinfo'

class DDetailVV(DetailView):
    #设置模版文件
    template_name = 'DDetailV.html'
    #设置模型外的数据
    extra_context = {'title':'boke'}
    #设置模型的查询字段
    slug_field ='title'
    #设置路由的变量名，与属性slug_field实现模型的查询操作
    slug_url_kwarg='title'
    pk_url_kwarg = 'pk'
    #设置查询模型PersonInfo
    model = Post
    #属性queryset可以做简单的查询操作
    #queryset=PersonInfo.objects.all()
    #若不设置，则模版上下文默认为persioninfo
    #context_object_name='personinfo'
    #是否将pk和slug作为查询条件
    #query_pk_and_slug=False

def result(request):
    return HttpResponse('Oh耶，成功了！')

class FFormVV(FormView):
    initial={'title':'深入理解错误恢复','content':'从场景出发,我们知道写文件的流程是这样的','author':'哈哈哈'}
    template_name = 'FormVV.html'
    success_url = '/result'
    form_class = PostForm
    extra_context = {'title':'boke'}

class CCreateVV(CreateView):
    initial={'title':'深入理解错误恢复','content':'从场景出发,我们知道写文件的流程是这样的','author':'哈哈哈'}
    template_name = 'FormVV.html'
    success_url = '/result'
    #表单生成方式一
    #form_class = PersonInfoForm
    #表单生成方式二
    model = Post
    #fields设置模型字段，从而生成表单字段
    fields = ['title','content','author']
    extra_context = {'title':'boke'}

class UUpdateVV(UpdateView):
    template_name = 'UUpdateV.html'
    success_url = '/result'
    model = Post
    #fields设置模型字段，从而生成表单字段
    fields = ['title','content','author']
    slug_url_kwarg = 'title'
    slug_field = 'title'
    context_object_name = 'post'
    extra_context = {'title':'boke'}

class DDeleteVV(DeleteView):
    template_name = 'DDeleteV.html'
    success_url = '/result'
    model = Post
    context_object_name = 'post'
    extra_context = {'title': 'boke'}
# Create your views here.
